package com.obrempon.credit.market.utils.helper

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.obrempon.credit.market.ObremponApp
import com.obrempon.credit.market.common.CommonActivity
import com.obrempon.credit.market.event.ObremponEventPointHelper
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.sdk.analyse.util.ObremponReviewUtil
import com.obrempon.credit.market.ui.activity.ObremponUserFeedbackAct
import com.obrempon.credit.market.ui.dialog.ObremponScoreDialog
import com.obrempon.credit.market.utils.log.ObremponLogger
import com.obrempon.credit.market.utils.shared.ObremponSharedHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedKey
import org.json.JSONObject

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 16:26
 * @Annotation：评分帮助类
 */
object ObremponScoreDialogHelper {

    /**
     * 展示评分
     */
    fun showStarDialog(context: Context, data: JSONObject, isNeedAdd: Boolean){

        if (data.has("activeWindow") && data.optJSONObject("activeWindow") != null){
            var isShowActiveDialog = false
            val activeWindowObj = data.optJSONObject("activeWindow")
            //需要弹出
            if (activeWindowObj != null && activeWindowObj.optString("switch") == "YES"){
                if (isNeedAdd){//是否需要判断出现次数
                    val totalNum = activeWindowObj.optInt("num")
                    val curtNum = ObremponSharedHelper.queryObremponSharedInt(ObremponSharedKey.OBREMPON_DIALOG_ACTIVE_NUM)
                    if (curtNum < totalNum){
                        isShowActiveDialog = true
                        ObremponSharedHelper.addObremponSharedData(ObremponSharedKey.OBREMPON_DIALOG_ACTIVE_NUM,curtNum+1)
                    }
                } else {
                    isShowActiveDialog = true
                }
            }
            if (isShowActiveDialog){
                showActiveStarDialog(context,activeWindowObj!!.optString("content"))
                return
            }
        }

        if (data.has("normalWindow") && data.optJSONObject("normalWindow") != null){
            val normalWindowObj = data.optJSONObject("normalWindow")
            if (isNeedAdd){
                if (normalWindowObj != null && normalWindowObj.optString("switch") == "YES"){
                    val totalNum = normalWindowObj.optInt("num")
                    val curtNum = ObremponSharedHelper.queryObremponSharedInt(ObremponSharedKey.OBREMPON_DIALOG_NORMAL_NUM)
                    if (curtNum >= totalNum){
                        return
                    }
                    ObremponSharedHelper.addObremponSharedData(ObremponSharedKey.OBREMPON_DIALOG_NORMAL_NUM,curtNum+1)
                    showNormalStarDialog(context,normalWindowObj.optString("content"))
                }
            } else {//个人中心
                showNormalStarDialog(context,normalWindowObj!!.optString("content"))
            }
        }
    }

    /**
     * activeWindow
     */
    private fun showActiveStarDialog(context: Context, tips: String){
        showNormalStarDialog(context,tips)
    }

    /**
     * normalWindow
     */
    private fun showNormalStarDialog(context: Context, tips: String){
        val normalDialog = ObremponScoreDialog(context,object : IStarDialogCallback{
            override fun completeScoringCount(count: Int) {
                ObremponLogger.e(">>>>>>>>>>score num：$count")
                ObremponEventPointHelper.addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_SCORE_RATE,count.toString())
                /**
                 * 数量 >= 4  ->  跳转Google 评分 （AppReviewUtil.launchReviewFlow() ）
                 * 数量 < 4   ->  跳转意见反馈  ( UserFeedbackAct )
                 */
                if (count < 4){
                    ObremponUserFeedbackAct.start(context, count.toString())
                } else {
                    val act = context as CommonActivity
                    ObremponReviewUtil.launchReviewFlow(act, completed = {
                        ObremponLogger.e(">>>>>completed>>>>>>>>>>$it")
                        if (!it){
                            //跳转到应用市场
                            enterMarket()
                        }
                    })
                }
            }
        })
        normalDialog.setCancelable(true)
        normalDialog.setCanceledOnTouchOutside(false)
        normalDialog.show()
        normalDialog.showObremponScoreTipsText(tips)
    }

    /**
     * 跳转到应用商店
     */
    private fun enterMarket(){
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse("https://play.google.com/store/apps/details?id=${ObremponApp.app.packageName}")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        ObremponApp.app.startActivity(intent)
    }

    interface IStarDialogCallback {
        //返回评星数量
        fun completeScoringCount(count: Int)
    }
}